package com.sl.transport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.sl.transport.entity.node.BaseEntity;
import com.sl.transport.repository.BaseRepository;
import com.sl.transport.service.IService;
import org.springframework.beans.factory.annotation.Autowired;

public class ServiceImpl<M extends BaseRepository<T, Long>, T extends BaseEntity> implements IService<T> {

    @Autowired
    private M repository;

    @Override
    public T queryByBid(Long bid) {
        return this.repository.findByBid(bid).orElse(null);
    }

    @Override
    public T create(T t) {
        t.setId(null); //由neo4j生成
        return this.repository.save(t);
    }

    @Override
    public T update(T t) {
        //先查后改
        T data = this.queryByBid(t.getBid());
        if (null == data) {
            return null;
        }

        //将需要修改的字段值拷贝到对象中，拷贝时忽略空值
        BeanUtil.copyProperties(t, data, CopyOptions.create().ignoreNullValue().setIgnoreProperties("id", "bid"));

        return this.repository.save(data);
    }

    @Override
    public Boolean deleteByBid(Long bid) {
        return this.repository.deleteByBid(bid) > 0;
    }
}
